﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Common;

namespace CMN.Controllers.Filters
{
    public class ExceptionFilter : FilterAttribute,IExceptionFilter
    {
        void IExceptionFilter.OnException(ExceptionContext filterContext)
        {
            AppLog.Write(string.Concat("客户机IP:", filterContext.RequestContext.HttpContext.Request.UserHostAddress, "\r\n错误地址:", filterContext.RequestContext.HttpContext.Request.Url, "\r\n异常信息:"), AppLog.LogMessageType.Error, filterContext.Exception);
            filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
            filterContext.Result = new ViewResult()
            {
                ViewName = "Error",
                ViewData = filterContext.Controller.ViewData,
            };
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Write("{\"success\":\"false\",\"message\":\"" + filterContext.Exception.Message.Replace("\"", "").Replace("\r", "").Replace("\n", "") + "\"}");
            filterContext.HttpContext.Response.End();
        }
    }
}
